class Solution(object):
    def singleNonDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        if n == 1:
            return nums[0]
        left = 0
        right = n - 1
        while left < right:
            mid = (left + right) // 2
            if nums[mid] != nums[mid - 1] and nums[mid] != nums[mid + 1]:
                return nums[mid]
            if right - left == 2:
                if nums[mid] == nums[left]:
                    return nums[right]
                else:
                    return nums[left]
            else:
                if mid % 2 == 0:
                    if nums[mid] == nums[mid - 1]:
                        right = mid - 2
                    else:
                        left = mid + 2
                else:
                    if nums[mid] == nums[mid - 1]:
                        left = mid + 1
                    else:
                        right = mid - 1
        return nums[left]

data = Solution()
nums = [1, 1, 2, 3, 3, 4, 4, 8, 8]
print(data.singleNonDuplicate(nums))
nums = [1, 1, 2, 3, 3]
print(data.singleNonDuplicate(nums))
nums = [1, 1, 2, 2, 3]
print(data.singleNonDuplicate(nums))
